import uni_request from './uni_request.js'

const request = uni_request({
	timeout: 10000,
	baseURL: "http://ntkfqxmfw.cn:8088/api",
})
request.interceptors.request.use(async config => { // 请求拦截器
	config.header['Authorization'] = uni.getStorageSync('access_token')
	config.header['clientid'] = "e5cd7e4891bf95d1d19206ce24a7b32e";
	config.header['Content-Type'] = 'application/json;';
	return config
})

request.interceptors.response.use((response, ...args) => { // 响应拦截器
	if (response.statusCode === 200) {

	} else { // 服务器响应不为 200 的统一处理方法
		uni.showToast({
			title: '服务器响应异常',
			icon: 'none'
		})
	}
	return response
})

request.interceptors.response.use(async (response, ...args) => { // 响应拦截器
	if (response.data.code == 401) {
		uni.showToast({
			title: '登录已过期，请重新登录！',
			icon: 'none',
			duration: 1000
		})
		uni.reLaunch({
			url: '/pages/login/login',
		})
	}else if (response.data.code !=200) {
		uni.showToast({
			title: response.data.msg,
			icon: 'none',
			duration: 1000
		})
	}
	return response
})

request.onerror = async (...args) => { // 请求失败统一处理方法
	console.log('onerror', args)
}

export default request